

import { isHTMLTag, isDef } from '../../util/index'
import { VNode } from './vnode'
import { resolveAsset } from '../util/options'
import { createComponent } from './create-component'


export function createElement(
    context,
    tag,
    data,
    children,
    normalizationType) {

    if (normalizationType === 2) {
        children = simpleNormalizeChildren(children)
    }
    let vnode
    let Ctor
    if (typeof tag === 'string') {
        if (isHTMLTag(tag)) {
            vnode = new VNode(tag, data, children, undefined, undefined, context)
        } else if (isDef(Ctor = resolveAsset(context.$options, 'components', tag))) {
            vnode = createComponent(Ctor, data, context, children, tag)
        } else {  //未知类型的标签
            vnode = new VNode(tag, data, children, undefined, undefined, context)
        }
    }
    return vnode
}


function simpleNormalizeChildren(children) {
    const childArr = []
    for (const child of children) {
        if (Array.isArray(child)) {
            childArr.push.apply(childArr,simpleNormalizeChildren(child)) 
        } else {
            childArr.push(child)
        }
    }
    return childArr
}